<?php
/**
 * Created by PhpStorm.
 * User: dreamy
 * Date: 2019-05-30
 * Time: 16:11
 */
namespace common\models;

use common\services\TallyLogService;
use common\services\TallyPmService;
use common\services\UserService;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

/**
 * This is the model class for table "tally".
 *
 * @property int $id 账本
 * @property string $title 名称
 * @property int $uid 创建者
 * @property int $type 账套类型
 * @property int $financial_type 公司财务类型 1仅有财务 2财务&出纳
 * @property string $pic 账套图片
 * @property string $company_title 账套图片
 * @property string $province 省份
 * @property string $city 城市
 * @property string $industry 所属行业
 * @property string $memo 备注
 * @property string $assert_amount 总资产
 * @property string $debt_amount 总负债
 * @property string $created_time 创建时间
 * @property string $updated_time 更新时间
 */
class Tally extends \yii\db\ActiveRecord
{
    // 财务类型 仅有会计 无审核流程
    const FINANCIAL_TYPE_SIMPLE = 1;

    // 财务类型 会计+出纳 有审核流程
    const FINANCIAL_TYPE_AUDIT= 2;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tally';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'type','financial_type'], 'integer'],
            [['uid', 'title'], 'required'],
            [['assert_amount', 'debt_amount','express_time'], 'number'],
            [['created_time', 'updated_time','state'], 'safe'],
            [['province', 'city','industry'], 'string'],
            [['title','company_title'], 'string', 'max' => 60],
            [['pic', 'memo'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'uid' => 'Uid',
            'type' => 'Type',
            'financial_type' => 'Financial Type',
            'pic' => 'Pic',
            'province' => 'Province',
            'city' => 'City',
            'memo' => 'Memo',
            'company_title' => 'Company Title',
            'assert_amount' => 'Assert Amount',
            'debt_amount' => 'Debt Amount',
            'created_time' => 'Created Time',
            'updated_time' => 'Updated Time',
            'state' => 'State 状态'
        ];
    }

    /**
     * @param bool  $insert
     * @param array $changedAttributes
     *
     */
    public function afterSave($insert, $changedAttributes)
    {
        $this->on(self::EVENT_AFTER_INSERT,['common\models\Tally', 'bindPm'],$this->getAttributes());
        parent::afterSave($insert, $changedAttributes);
    }

    /**
     * @param $data
     * @desc 账本创建完成后
     */
    public static function bindPm($data)
    {
        $handleData = ArrayHelper::toArray($data);
        $tallyId    = ArrayHelper::getValue($handleData,'data.id');
        $uid        = ArrayHelper::getValue($handleData,'data.uid');
        // 绑定创建者与账本关系
        TallyPmService::create([
            'tally_id' => $tallyId,
            'uid'   => $uid,
            'role'  => TallyPm::ROLE_ADMIN
        ]);
    }

    /**
     * @param $event \common\event\TallyEvent
     * @return mixed
     * @desc 记录账本相关日志
     */
    public static function recordLog($event)
    {
        $logData = $event->logData ;
        if(!$logData['uid'] || !$logData['content'] ) {
            return true ;
        }
        // 根据uid 获取username
        $userInfo = UserService::getUserByUid($logData['uid']);
        $logData['username'] = $userInfo['username'] ?: $userInfo['mobile'] ;
        TallyLogService::create($logData);
        Yii::info('记录账本日志:'.Json::encode($logData,JSON_UNESCAPED_UNICODE));
        return true ;
    }
}
